<template>
    <div class="title-wrapper">
        <el-skeleton animated :loading="loading">
            <template #template>
                <el-skeleton-item />
                <el-skeleton-item />
                <el-skeleton-item />
                <el-skeleton-item />
            </template>
            <template #default>
                <div class="title-box" v-if="detailData">
                    <div class="left-title">
                        <span class="title">{{ detailData.displayName }}</span>
                        <el-tag type="warning">{{ modifierType[detailData.modifierType] }}</el-tag>
                        <el-tag :type="detailData.activeState === 'ACTIVATED' ? 'success' : 'info'">
                            {{ detailData.activeState ? activeState[detailData.activeState] : '--' }}
                        </el-tag>
                    </div>
                    <div class="right-title">
                        <el-button
                            type="primary"
                            v-if="detailData.activeState === 'NEW' && detailData.modifierType !== 'EXTENDED'"
                            @click="operCollect('激活', detailData)"
                            >激活</el-button
                        >
                        <el-button @click="operCollect('返回')">返回</el-button>
                    </div>
                </div>
            </template>
        </el-skeleton>
    </div>
</template>
<script setup lang="ts">
import { useRouter } from 'vue-router';
import { activeState, modifierType } from '@/utils/constant';
import type { SysMetadataObject } from '@great/services';
import { modelThingService } from '@great/services';

const props = defineProps<{
    detailData?: SysMetadataObject;
}>();
const router = useRouter();
const initModelDetail: any = inject('initModelDetail');

const loading = ref(true);

/**
 * 操作收集
 * @param type 操作类型
 * @param row 操作行数据
 */
const operCollect = (type: string, row?: SysMetadataObject) => {
    switch (type) {
        case '返回':
            router.push('/model/business/list');
            break;
        case '激活':
            ElMessageBox.confirm(`是否确认激活模型 ${row?.displayName} ？`, '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    await modelThingService.extendedObject({
                        objectNames: [row?.objectName as string],
                        extendedObject: true,
                        createType: 1
                    });
                    ElNotification({
                        title: '激活成功！',
                        type: 'success'
                    });
                    initModelDetail();
                })
                .catch(() => {});
            break;
    }
};

watch(
    () => props.detailData,
    newValue => {
        if (newValue) loading.value = false;
    },
    { immediate: true }
);
</script>
<style scoped lang="scss">
.title-wrapper {
    .el-skeleton {
        display: flex;
        .el-skeleton__item {
            width: 20%;
            height: 32px;
        }
        .el-skeleton__item + .el-skeleton__item {
            margin-left: 10px;
            width: 60px;
        }
    }
    .title-box {
        display: flex;
        justify-content: space-between;
        height: 32px;
        align-items: center;
        .left-title {
            display: flex;
            align-items: center;
            .title {
                font-size: 18px;
                font-weight: 600;
            }
            .number {
                color: #409eff;
            }
            > span + span {
                margin-left: 10px;
            }
        }
    }
}
</style>
